package edu.xmut.yuepai.publish.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xmut.yuepai.client.BlogFeignClient;
import edu.xmut.yuepai.client.SysPictureFeignClient;
import edu.xmut.yuepai.common.utils.IDGenerator;
import edu.xmut.yuepai.common.utils.IpUtils;
import edu.xmut.yuepai.common.utils.UserContext;
import edu.xmut.yuepai.model.entity.Publish;
import edu.xmut.yuepai.model.entity.PublishForte;
import edu.xmut.yuepai.model.form.BlogPublishForm;
import edu.xmut.yuepai.model.form.YuePaiPublishForm;
import edu.xmut.yuepai.publish.mapper.PublishMapper;
import edu.xmut.yuepai.publish.service.PublishForteService;
import edu.xmut.yuepai.publish.service.PublishService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author shuofeng
 * @description 针对表【publish(发布约拍表)】的数据库操作Service实现
 * @createDate 2024-09-30 10:31:59
 */
@Service
public class PublishServiceImpl extends ServiceImpl<PublishMapper, Publish>
        implements PublishService {

    @Resource
    private BlogFeignClient blogFeignClient;

    @Resource
    private SysPictureFeignClient sysPictureFeignClient;

    @Resource
    private PublishForteService publishForteService;


    @Override
    public Long publishBlog(BlogPublishForm blogPublishForm, HttpServletRequest request) {
        // 获取远程IP
        String ipAddress = IpUtils.getIpAddress(request);
        return blogFeignClient.publish(blogPublishForm, ipAddress);
    }

    @Override
    public Long publishYuePai(YuePaiPublishForm form, HttpServletRequest request) {
        String content = form.getContent();
        BigDecimal longitude = form.getLongitude();
        BigDecimal latitude = form.getLatitude();
        String address = form.getAddress();
        String fullAddress = form.getFullAddress();
        List<String> imageIds = form.getImageIds();
        String price = form.getPrice();
        List<Long> forteIds = form.getForteIds();
        // 创建发布约拍对象
        Publish publish = new Publish();
        // 1.生成ID
        long id = IDGenerator.nextId();
        publish.setId(id);
        // 设置作者
        publish.setUserId(UserContext.getUserId());
        // 2.设置内容
        publish.setContent(content);
        // 3.设置发布经纬度
        publish.setLat(latitude);
        publish.setLon(longitude);
        // 4.设置约拍的地址
        publish.setAddress(address);
        publish.setFullAddress(fullAddress);
        // 5.设置价格
        publish.setPrice(new BigDecimal(price));
        // 6.设置封面
        // 7.设置关联的图片
        sysPictureFeignClient.saveBatchYuePaiPicture(id, imageIds);
        // 8.设置关联的特长
        List<PublishForte> publishForteList = forteIds.stream().map(forteId -> {
            PublishForte publishForte = new PublishForte();
            publishForte.setPublishId(id);
            publishForte.setForteId(forteId);
            return publishForte;
        }).toList();
        publishForteService.saveBatch(publishForteList);
        // 9.设置已发布状态
        publish.setStatus(1);
        // 返回id，并保存
        this.save(publish);
        return id;
    }
}




